<template>
    <el-dialog v-model="dialogVisible" align-center width="50%" :before-close="handleClosed">
        <el-form inline>
            <el-form-item label="库区编码">
                <el-input v-model="form.areaCode" placeholder="请输入区域名称"></el-input>
            </el-form-item>
            <el-form-item label="库区名称">
                <el-input v-model="form.areaName" placeholder="请输入区域名称"></el-input>
            </el-form-item>
            <el-form-item label="库区面积">
                <el-input-number v-model="form.wareArea" min="0" placeholder="请输入区域名称"></el-input-number>
            </el-form-item>

        </el-form>
        <el-form-item label="备注">
            <el-input v-model="form.areaRemark" type="textarea" placeholder="请输入区域名称"></el-input>
        </el-form-item>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handleClosed">取 消</el-button>
                <el-button type="primary" @click="handleAdd">确 定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
import wareAreaAPI from "@/util/request-md/ware/ware-area.js";

export default {
    emits: ['callClosed', 'callSuccess'],
    props: ['info', 'visible', 'isEdit', 'wareId'],
    name: "MakeWareAreaDialog",
    data() {
        return {
            dialogVisible: false,
            form: {
                areaCode: '',
                areaName: '',
                areaRemark: '',
                wareId: '',
                wareArea: 0
            }
        }
    },
    watch: {
        'visible'(val) {
            this.dialogVisible = val;
        },
        'isEdit'(val) {
            if (val) {
                this.form = {
                    wareAreaId: this.info.wareAreaId,
                    areaCode: this.info.areaCode,
                    areaName: this.info.areaName,
                    areaRemark: this.info.areaRemark,
                    wareId: this.info.wareId,
                    wareArea: this.info.wareArea
                }
            } else {
                this.form = {
                    areaCode: '',
                    areaName: '',
                    areaRemark: '',
                    wareId: '',
                    wareArea: 0
                }
            }
        }
    },
    methods: {
        handleClosed() {
            this.$emit("callClosed");
        },
        handleAdd() {
            this.form.wareId = this.wareId;
            if (this.isEdit) {
                console.log(this.form,">>>>即将更新的数据this.form")
                wareAreaAPI.updateWareArea(this.form, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '修改成功'
                        })
                        this.$emit('callSuccess');
                    } else {
                        this.$message.error(`网络异常:${res.msg}`);
                    }
                })
            } else {
                wareAreaAPI.addWareArea(this.form, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '添加成功'
                        })
                        this.$emit('callSuccess');
                    } else {
                        this.$message.error(`网络异常:${res.msg}`);
                    }
                })
            }

        }
    }
}
</script>

<style scoped>

</style>